/**
 * ----------------------------------------------------------------------------
 * Copyright Jens Konopka
 * 
 * Alle Rechte vorbehalten.
 * Licence: Apache 2.0
 * ----------------------------------------------------------------------------
 *
 * created on 05.03.2012 by jkonopka
 */
package de.sql.toolkit.exporter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;


/**
 * @author jkonopka
 *
 */
abstract class AbstractExporter implements IExporter {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8025363836431183931L;
	private File dstPath;
	private String schema;
	
	/**
	 * @param dstPath
	 * @param schema
	 */
	protected AbstractExporter(File dstPath, String schema) {
		this.dstPath = dstPath;
		this.schema = schema;
	}
	
	/**
	 * @param filename
	 * @param content
	 * @throws Exception
	 */
	protected final void writeContentToFile(final String filename, final String content) throws Exception {
		final File file = new File(filename);
		final OutputStream fos = new FileOutputStream(file);
		final Writer out = new OutputStreamWriter(fos);
		try {
			out.write(content);
		} finally {
			out.close();
		}
	}
	
	/**
	 * @throws Exception
	 */
	protected void validateDstPath() throws Exception {
		if (getDstPath() == null) {
			throw new NullPointerException();
		}

		if (!getDstPath().exists()) {
			throw new IOException(String.format("The Path %s must exist!", getDstPath()));
		}

		if (!getDstPath().isDirectory()) {
			throw new IOException(String.format("The Path %s must be a directory!", getDstPath()));
		}
	}
	
	/**
	 * @return the dstPath
	 */
	protected final File getDstPath() {
		return dstPath;
	}

	/**
	 * @param dstPath the dstPath to set
	 */
	protected final void setDstPath(File dstPath) {
		this.dstPath = dstPath;
	}

	/**
	 * @return the schema
	 */
	protected final String getSchema() {
		return schema;
	}

	/**
	 * @param schema the schema to set
	 */
	protected final void setSchema(String schema) {
		this.schema = schema;
	}

}
